<?php
/**
 * Copyright (c) 2022-2222   All rights reserved.
 *
 * 创建时间：2022-04-06 16:07
 *
 * 项目：levs  -  $  - generatePagesHelper.php
 *
 * 作者：liwei
 */

//!defined('INLEV') && exit('Access Denied LEV');


namespace modules\levmodules\helpers;

use Lev;
use lev\helpers\ModulesHelper;

class generatePagesHelper extends generateHelper
{

    public static function createPagesController($iden, $pageNavs, $replaces = []) {
        if ($pageNavs) {
            foreach ($pageNavs as $v) {
                if (empty($v['gen']) && $v['link']) {
                    $route = explode("/", $v['link'])[0];
                    static::generates($iden, $route, $replaces, !$v['forceGen']);
                }
            }
            static::$tips && Lev::setNotices(static::$tips, true);
        }
    }

    public static function generates($iden, $route, $replaces = [], $forceGen = false) {
        $ckModule = static::isExistIden($idenDir = ModulesHelper::getIdenDir($iden));
        if (!$ckModule) {
            static::$tips .= '请先创建模块 ';
            return Lev::responseMsg(-10111, '请先创建模块');
        }
        $moduleIdenDir = Lev::$aliases['@modules'] . '/' . $idenDir;
        $className = Lev::ucfirstv($route);
        $ckController = $moduleIdenDir . '/controllers/' . $className . 'Controller.php';
        if (!$forceGen && is_file($ckController)) {
            static::$tips .= ' <tips>页面控制器已经创建过了！</tips>'.basename($ckController).'<br>';
            return Lev::responseMsg(-10111, '控制器已经创建过了！'.$ckController);
        }else if (is_file($ckController)) {
            static::$tips .= ' <tips>抱歉，控制器已存在！请手动删除再执行！</tips>'.($ckController).'<br>';
            return Lev::responseMsg(-10100, '抱歉，控制器已存在！请手动删除再执行!'.$ckController);
        }
        $generateDir = dirname(__DIR__) . '/_generates/_pages/_tab_view/_levcc';
        $replaces = static::replaceVars($iden, $replaces);
        $replaces['ZstXt']       = $className;
        $replaces['zst-xt']      = $route;
        $replaces['zst_xt']      = $widgetFolder = str_replace('-', '_', $route);
        $unFiles = [];
        //$unFiles[$ckController] = 1;
        static::doGenerateFiles($generateDir, $moduleIdenDir, array_keys($replaces), array_values($replaces), $unFiles);
        static::$tips .= ' 页面控制器文件创建成功：'.basename($ckController).'<br>';
        static::$tips .= ' 部件文件夹创建成功：widgets/'.$widgetFolder.'/。共计2处<br>';
        static::$tips .= ' 访问前台页面：<a target="_blank" _bk="1" href="'.Lev::toReRoute([$route, 'id'=>$iden]).'">'.$route.'</a><br>';
        static::$tips .= '生成文件详细清单：'.static::showGenFile(static::$generateFiles).'<br>';
        return Lev::responseMsg(1, '', ['files'=>static::$generateFiles]);
    }

    public static function showGenFile($files) {
        $string = '';
        foreach ($files as $v) {
            if (is_file($v)) {
                $string .= '<div class="font12 scale8 transl nowrap">'. $v . '</div><br>';
            }
        }
        return $string;
    }
}